package com.universityJob.modules.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.universityJob.common.utils.*;
import com.universityJob.modules.data.dao.DataRecDao;
import com.universityJob.modules.data.entity.DataJobEntity;
import com.universityJob.modules.data.entity.DataRecEntity;
import com.universityJob.modules.data.service.DataRecService;
import net.sf.json.JSONArray;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * @author Administrator
 */
@Service("dataRecService")
public class DataRecServiceImpl extends ServiceImpl<DataRecDao, DataRecEntity> implements DataRecService {
    @Autowired
    DataRecDao dataRecDao;
    @Autowired
    Upload upload;
    @Autowired
    private ExportExcel exportExcel;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String recname = (String) params.get("recname");
        IPage<DataRecEntity> page = this.page(
                new Query<DataRecEntity>().getPage(params),
                new QueryWrapper<DataRecEntity>().
                        like(StringUtils.isNotBlank(recname), "recName", recname)
        );

        return new PageUtils(page);
    }

    @Override
    public DataRecEntity queryByRecName(String recname) {
        return null;
    }

    @Override
    public DataRecEntity selectRecByOid(String id) {
        try {
            DataRecEntity dataRecEntity = dataRecDao.selectRecByOid(id);
            return dataRecEntity;
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public R saveRecs(List<DataRecEntity> recs) {
        dataRecDao.insertRecs(recs);
        return null;
    }

    @Override
    public R saveRec(DataRecEntity rec) {
        try {
            List<Map> byName = dataRecDao.findByName(rec.getRecname());
            if (byName.size() == 0) {
                rec.setPublishdate(new Date());
                dataRecDao.insertRec(rec);
                return R.ok("新增成功！！");
            } else {
                return R.error("此企业已存在！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("新增异常");
    }

    @Override
    public R updateRec(DataRecEntity rec) {
        return null;
    }


    @Override
    public R deleteRec(String[] recIds) {
        boolean remove = this.removeByIds(Arrays.asList(recIds));
        if (remove) {
            for (int i = 0; i < recIds.length; i++) {
                boolean b = upload.deleteFile(recIds[i].toString() + ".jpg");
                if (!b) {
                    log.error("删除企业id为 " + recIds[i] + " 的logo删除失败");
                }
            }
            return R.ok();
        }
        return R.error("删除出错，请联系管理员！");
    }

    @Override
    public boolean truncateRec() {
        boolean bl = dataRecDao.truncate();
        return bl;
    }

    @Override
    public R findByName(String recname) {
        List<Map> byName = dataRecDao.findByName(recname);
        return R.ok().put("rec", byName);
    }

    @Override
    public int exportStatisticsReport(HttpServletResponse response, Map<String, Object> params) {
        //获取数据
        List<Map<String, Object>> maps = dataRecDao.selectMaps(null);
        int i = exportExcel.excelInit(maps, response, params);
        if (i == 0) {
            //excel导出失败！
            return 0;
        }
        //以上均为数据准备,下面开始调用导出excel工具类
        return 1;
    }

    @Override
    public int inputExcel(HttpServletResponse response, Map<String, Object> params) {
        String recs = MapUtils.getString(params, "rec", "");
        int count = 0;
        List<DataRecEntity> list = JSON.parseArray(recs, DataRecEntity.class);//list json字符串转
        for (DataRecEntity rec : list) {
            rec.setPublishdate(new Date());
            try {
                dataRecDao.insert(rec);
                count += 1;
            } catch (Exception e) {
                logger.error("插入id为 {} 数据失败",rec.getRecid());
            }
        }
        return count;
    }
}
